package memo.controller;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.google.gson.Gson;

import memo.model.FolderDTO;
import memo.model.MemberDTO;
import memo.model.MemoDAO;
import memo.model.MemoDTO;

@WebServlet("*.do")
public class MemoController extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	MemoDTO Mdto = new MemoDTO();
	FolderDTO Fdto = new FolderDTO();
	MemberDTO dto = new MemberDTO();
	MemoDAO dao = MemoDAO.getInstance();
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String requestURI = request.getRequestURI();
		String contextPath = request.getContextPath();
		String url = requestURI.substring(contextPath.length());
		HttpSession session = request.getSession();
		System.out.println("get 접근 : " + url);
		PrintWriter out = response.getWriter();
		
		if(url.equals("/main.do")) {        			
			if(session.getAttribute("id") != null) {
				
				out.print(session.getAttribute("id"));
				
			}else {
				
				out.print("");
				
			}
			out.close();
		}else if(url.equals("/logout.do")) {    //로그아웃

			session.invalidate(); 
			
		}else if(url.equals("/getInfo.do")) {

		     String id = (String) session.getAttribute("id");
		 
		     dto = dao.getInfo(id);
			 
		     Gson gson = new Gson();
		    
			 System.out.println( gson.toJson(dto));//자바 객체를 json으로 변환
		     out.print(gson.toJson(dto)); //회원정보 받아옴
		     out.close();
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String requestURI = request.getRequestURI();
		String contextPath = request.getContextPath();
		String url = requestURI.substring(contextPath.length());
		HttpSession session = request.getSession();
		
		System.out.println("post 접근 : " + url);
		PrintWriter out = response.getWriter();
		
		if(url.equals("/join.do")) {          //회원가입
	
			String id = (String)request.getParameter("id");
			String age = (String)request.getParameter("age");
			String name = (String) request.getParameter("name");
			String password1 = (String) request.getParameter("password1");
			String password2 = (String) request.getParameter("password2");
			int insertResult = 0;
	
			if(password1.equals(password2)) { //재입력한 비밀번호와 일치 여부
				
					dto.setId(id);
					dto.setAge(Integer.parseInt(age));
					dto.setName(name);
					dto.setPassword(password1);
					
					insertResult = dao.insertMember(dto);  //MemberDAO의 insertMember메소드를 호출해 성공 또는 에러값 저장
					
					
			        out.print(insertResult);
			        System.out.println(insertResult);
			        out.close();
		        		
			}else {
			
		        out.print('3');
		        out.close();
			}
			
		}else if(url.equals("/login.do")) {     //로그인
			
			String id = (String) request.getParameter("id");
			String password = (String) request.getParameter("password");
			int loginResult = 0;
			
			dto = dao.getInfo(id);
			String memberpass = dto.getPassword();
			String memberid = dto.getId();
			
		
			if(memberid == null) {
				loginResult = 10;
		
		        out.print(loginResult);
		        out.close();
			}else {
				if(memberpass.equals(password)) {
					session.setAttribute("id",memberid);
					session.setMaxInactiveInterval(600);
		
			        out.print(memberid);
			        out.close();
				}else {
				
			        out.print('3');
			        out.close();
				}
			}
		  
			
		}else if(url.equals("/delete.do")) {    //회원삭제
			System.out.println("삭제");
			
			String id = (String)session.getAttribute("id");
			System.out.println("세션id : " + id);
			int deleteResult = 13;
			
			dto.setId(id);
			if(dao.deleteMember(dto)){
				System.out.println("삭제성공");
				session.invalidate();
				
				out.print(deleteResult);
				out.close();
			
			}
	        
		}else if(url.equals("/update.do")) {        //회원정보수정
			
			String id = (String) session.getAttribute("id"); 
			System.out.println("id="+id);
			int age = Integer.parseInt(request.getParameter("age"));
			String password1 = (String) request.getParameter("password1");
			String password2 = (String) request.getParameter("password2");
			int insertResult = 0;
			
			if(password1.equals(password2)) {
				
				dto.setId(id);
				dto.setAge(age);
				dto.setPassword(password1);
				
				insertResult = dao.updateMember(dto);
				
				if(insertResult == -3) {
					
			        out.print(id);
			        out.close();
				}else {
					
					out.print(insertResult);
					out.close();
				}

			}else {
				
		        out.print('3');
		        out.close();
				
			}
		}else if(url.equals("/addFolder.do")) {           //폴더 저장
		
			String id = (String)request.getParameter("id");
			String foldername = (String)request.getParameter("foldername");
			
			
			Fdto.setId(id);
			Fdto.setFolderName(foldername);
			
			
			Fdto = dao.insertFolder(Fdto);  //MemberDAO의 insertMember메소드를 호출해 성공 또는 에러값 저장
			
			Gson gson = new Gson();
			
	        out.print(gson.toJson(Fdto));
	        System.out.println(gson.toJson(Fdto));
	        out.close();
	        
		}else if(url.equals("/updateFolder.do")) {           //폴더 업데이트
		
			String folderNum = (String)request.getParameter("folderNum"); 
			String foldername = (String)request.getParameter("folderName"); 
			System.out.println(folderNum);
			System.out.println(foldername);
			FolderDTO Fdto = new FolderDTO(foldername);
			Fdto.setFolderNum(Integer.parseInt(folderNum));
			
			int updateResult = dao.updateFolder(Fdto);
			
			
	        out.print(updateResult);
	        System.out.println(updateResult);
	        out.close();
	        
		}else if(url.equals("/removeFolder.do")) {           //폴더 삭제
		
			String folderNum = (String)request.getParameter("id"); 
			
			System.out.println(folderNum);
			FolderDTO Fdto = new FolderDTO(folderNum);
			Fdto.setFolderNum(Integer.parseInt(folderNum));
			
			int removeResult = dao.deleteFolder(Fdto);
			
			
	        out.print(removeResult);
	        System.out.println(removeResult);
	        out.close();
	        
		}else if(url.equals("/addMemo.do")) {           //메모 저장
		
			String folder_num = (String)request.getParameter("folder_num");
			ArrayList<MemoDTO> memolist = dao.insertMemo(folder_num);
			
			Gson gson = new Gson();
			
	        out.print(gson.toJson(memolist));
	        System.out.println(gson.toJson(memolist));
	        out.close();
	        
		}else if(url.equals("/updateMemo.do")) {           //메모 업데이트
		
			String id = (String)request.getParameter("id"); // memoID
			String content = (String)request.getParameter("content"); // memoContent
			System.out.println(content);
			Mdto.setMemoNum(Integer.parseInt(id));
			Mdto.setMemoContent(content);
			
			Mdto = dao.updateMemo(Mdto);
			Gson gson = new Gson();
			
	        out.print(gson.toJson(Mdto));
	        System.out.println(gson.toJson(Mdto));
	        out.close();
	        
		}else if(url.equals("/removeMemo.do")) {           //메모 삭제
		
			String memoNum = (String)request.getParameter("id"); 
			
			Mdto.setMemoNum(Integer.parseInt(memoNum));
			
			int removeResult = dao.deleteMemo(Mdto);
			
			
	        out.print(removeResult);
	        System.out.println(removeResult);
	        out.close();
	        
		} else if (url.equals("/getMemoList.do")) {
		
			
			String folderName = (String)request.getParameter("folderName");
			String folderNum = (String)request.getParameter("folderNum"); 
			ArrayList<MemoDTO> memolist = dao.getMemos( folderName ,folderNum);
			
			Gson gson = new Gson();
			
			out.print(gson.toJson(memolist));
	        System.out.println(gson.toJson(memolist));
	        out.close();
		}else if (url.equals("/getFolderList.do")) {

			
			String id = (String) session.getAttribute("id"); // id
			ArrayList<FolderDTO> folderlist = dao.getFolders(id);
		
		    Gson gson = new Gson();
     
			out.print(gson.toJson(folderlist));
	        System.out.println(gson.toJson(folderlist));
	        out.close();
		}
	}
}
